package com.molyfun.parents.activity.store.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.store.TeacherProfileActivity;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.bean.ProductDetailInfoResponse;
import com.molyfun.parents.rest.bean.TeacherDetailResponse;
import com.molyfun.parents.utils.Const;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.carbs.android.expandabletextview.library.ExpandableTextView;
import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProductDetailInfoFragment extends BaseFragment {

    @InjectView(R.id.product_introduce)
    ExpandableTextView mProductIntroDuce;
    @InjectView(R.id.teacher_list_layout)
    LinearLayout mTeacherListLayout;

    private ProductDetailInfoResponse.ProductDetailInfoBean mProductDetailBean;

    public static ProductDetailInfoFragment newInstance(ProductDetailInfoResponse.ProductDetailInfoBean productDetailBean) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Const.PRODUCT_DETAIL_BEAN, productDetailBean);
        ProductDetailInfoFragment productDetailInfoFragment = new ProductDetailInfoFragment();
        productDetailInfoFragment.setArguments(bundle);
        return productDetailInfoFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_detail_info, container, false);
        ButterKnife.inject(this, view);

        mProductDetailBean = getArguments().getParcelable(Const.PRODUCT_DETAIL_BEAN);
        if (mProductDetailBean != null) {
            if (TextUtils.isEmpty(mProductDetailBean.getIntroduce())) {
                mProductIntroDuce.setText("无");
            } else {
                mProductIntroDuce.setText(mProductDetailBean.getIntroduce());
            }
        }

        getProductDetailInfo(mProductDetailBean.getId());

        return view;
    }

    private void getProductDetailInfo(String productId) {
        showProgressDialog();
        Call<ProductDetailInfoResponse> call = MFPApplication.getInstance().getRestClient().getProductDetailInfo(productId);
        call.enqueue(new Callback<ProductDetailInfoResponse>() {
            @Override
            public void onResponse(Call<ProductDetailInfoResponse> call, Response<ProductDetailInfoResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getCode() == 200 && response.body().getData() != null) {
                        initTeacherListLayout(response.body().getData().getUserList());
                    } else {
                        toast(response.body().getMsg());
                    }
                } else {
                    toast("获取套餐详情失败");
                }
            }

            @Override
            public void onFailure(Call<ProductDetailInfoResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void initTeacherListLayout(List<TeacherDetailResponse.TeacherDetailBean> teacherDetailBeanList) {
        mTeacherListLayout.removeAllViews();
        if (teacherDetailBeanList != null && teacherDetailBeanList.size() > 0) {
            int count = 0;
            for (final TeacherDetailResponse.TeacherDetailBean teacherDetailBean : teacherDetailBeanList) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_product_detail_teacher_cell, null, false);
                CircleImageView circleImageView = (CircleImageView) view.findViewById(R.id.avator);
                TextView teacherName = (TextView) view.findViewById(R.id.teacher_name);
                TextView teacherIdentity = (TextView) view.findViewById(R.id.teacher_identity);
                ExpandableTextView teacherIntroduce = (ExpandableTextView) view.findViewById(R.id.teacher_introduce);
                View mLine = view.findViewById(R.id.line);

                teacherName.setText(teacherDetailBean.getUsername());
                if (TextUtils.isEmpty(teacherDetailBean.getPost())) {
                    teacherIdentity.setVisibility(View.GONE);
                } else {
                    teacherIdentity.setVisibility(View.VISIBLE);
                    teacherIdentity.setText(teacherDetailBean.getPost());
                }

                if (TextUtils.isEmpty(teacherDetailBean.getSkills())) {
                    teacherIntroduce.setText("无");
                } else {
                    teacherIntroduce.setText(teacherDetailBean.getSkills());
                }

                if (!TextUtils.isEmpty(teacherDetailBean.getHeadimgurl())) {
                    Picasso.with(getContext())
                            .load(teacherDetailBean.getHeadimgurl())
                            .config(Bitmap.Config.RGB_565)
                            .placeholder(R.mipmap.ic_header_default)
                            .into(circleImageView);
                } else {
                    circleImageView.setImageResource(R.mipmap.ic_header_default);
                }
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TeacherProfileActivity.startActivity(getContext(), teacherDetailBean);
                    }
                });
                mTeacherListLayout.addView(view);
                if (count == (teacherDetailBeanList.size() - 1)) {
                    mLine.setVisibility(View.GONE);
                } else {
                    mLine.setVisibility(View.VISIBLE);
                }

                count++;

            }
        }
    }

}
